package com.iplatform.file.function.resourcedata.service.impl;


import com.iplatform.commons.utils.SnowflakeKeyGeneratorUtil;


import com.iplatform.file.function.meta.dao.FieldMetaMapper;
import com.iplatform.file.function.meta.dao.NeuralNetworkMapper;
import com.iplatform.file.function.meta.utils.JdbcUtil;
import com.iplatform.file.function.meta.vo.DbConfigVo;
import com.iplatform.file.function.resourcedata.entity.FileBeanDto;
import com.iplatform.file.function.quartz.domain.ResourceFileBean;
import com.iplatform.file.function.resourcedata.dao.FileDao;
import com.iplatform.file.function.resourcedata.dao.MenuDao;
import com.iplatform.file.function.resourcedata.entity.FileBean;
import com.iplatform.file.function.resourcedata.entity.MenuBean;
import com.iplatform.file.function.resourcedata.service.FileService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: FileServiceImpl
 * @description: TODO 类描述
 * @author: chensk
 * @date: 2020/12/3
 **/
@Service
public class FileServiceImpl implements FileService {
	
	
	/**
	 * 数据库字段前缀
	 */
	private final String DATAINDEX = "dataindex";
	
	/**
	 * 表名前缀
	 */
	private final String TABLE_PREFIX = "meta_";
	

	
	@Resource
	private NeuralNetworkMapper neuralNetworkMapper;

	@Autowired
	private SnowflakeKeyGeneratorUtil snowflakeKeyGeneratorUtil;
	
	
	@Resource
	private FieldMetaMapper fieldMetaMapper;

	@Resource
	private MenuDao menuDao;

	@Resource
	private FileDao fileDao;
	
	/**
	 * 声明变量 static
	 */
	public static Map<String, DataSource> dataSourceMap = new HashMap<>();
	
	
	@Override
	public int insert(FileBeanDto fileBean) {
		if (fileBean.getParentId() == 0) {
			Map<String, Object> map = new HashMap<>();
			map.put("menuName", fileBean.getMouldName());
			List<MenuBean> menuBeans = menuDao.selectChildByParentId(map);
			fileBean.setParentId(menuBeans.get(0).getId());
			return fileDao.insert(fileBean);
		}
		return fileDao.insert(fileBean);
	}
	
	@Override
	public List<FileBean> selectByCondition(Long parentId, String fileName) {
		
		return fileDao.selectByCondition(parentId, fileName);
	}
	
	@Override
	public FileBean selectDatabyId(Long id) {
		return fileDao.selectDatabyId(id);
	}
	
	@Override
	public int removeById(Long id) {
		return fileDao.deleteById(id);
	}
	
	
	
	@Override
	public List<ResourceFileBean> queryFileData(String sourceDir) {
		return fieldMetaMapper.queryFileData(sourceDir);
	}
	
	
	
	

	
	public DataSource getDataSource(String dbConfigId) {
		DataSource dataSource = dataSourceMap.get(dbConfigId);
		// 获取数据库配置
		if (dataSource == null) {
			DbConfigVo dbConfigVo = neuralNetworkMapper.queryById(dbConfigId);
			dataSource = JdbcUtil.getDataSource(dbConfigVo);
			dataSourceMap.put(dbConfigId, dataSource);
		}
		return dataSource;
	}
	

	
}
